package com.xuxingcan.findmyvoice.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.rey.material.widget.EditText;
import com.xuxingcan.findmyvoice.R;

/**
 * Created by xuxingcan on 16-4-3.
 */
public class LoginActivity extends BaseActivity {
    private EditText nickEditText, passwordEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        nickEditText = (EditText) findViewById(R.id.nick_name);
        passwordEditText = (EditText) findViewById(R.id.password);
        findViewById(R.id.sign_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, SignUpActivity.class));
            }
        });
        findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = nickEditText.getText().toString().trim();
                String passwd = passwordEditText.getText().toString().trim();
                if (userName == null || userName.length() == 0) {
                    showDialog("发生了一点错误", "用户名不能为空", "我知道了");
                    return;
                }
                if (passwd == null || passwd.length() == 0) {
                    showDialog("发生了一点错误", "密码不能为空", "我知道了");
                    return;
                }
                AVUser.logInInBackground(userName, passwd, new LogInCallback() {
                    public void done(AVUser user, AVException e) {
                        if (e == null) {
                            // 登录成功
                            showShortToast("登录成功");
                            finish();
                        } else {
                            if (e.getCode() == 211) {
                                showDialog("登录失败", "找不到该用户名", "我知道了");
                                return;
                            }
                            if (e.getCode() == 210) {
                                showDialog("登录失败", "密码错误", "我知道了");
                                return;
                            }
                            // 登录失败
                            showDialog("登录失败", e.getMessage(), "我知道了");
                        }
                    }
                });
            }
        });
    }
}
